<template>
  <div>
    <div class="base_header" style="text-align: left;margin-bottom: 20px">
      <span style="font-size: 30px;font-weight: normal;">
        方向管理
      </span>
      <span style="font-size: 10px">课程方向管理</span>
      <el-button type="success"
                 style="float: right"
                 @click="dialogVisible = true"
      >新增</el-button>
    </div>

    <el-dialog
      title="新增方向"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="方向名称">
          <el-input v-model="name" placeholder="请输入方向名称">
          </el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button @click="addCancle">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="directionList"
      style="width: 100%">
      <el-table-column
        label="序号"
        width="80">
        <template slot-scope="scope">
          <span>
            {{scope.$index + 1 +  (page - 1) * pageSize}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="名称">
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            {{scope.row.name}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div v-if="!showEdit[scope.$index]">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-popconfirm
              title="确定删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                size="mini"
                slot="reference"
                type="danger"
                >删除</el-button>
            </el-popconfirm>
          </div>
          <div v-else>
            <el-button
              size="mini"
              type="danger"
              @click="handleCancle(scope.$index, scope.row)">取消</el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleSave(scope.$index, scope.row)">保存</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="text-align: center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :hide-on-single-page="true"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DirectionManage',
  data () {
    return {
      total: 0,
      page: 1,
      pageSize: 10,
      dialogVisible: false,
      name: '',
      showEdit: [],
      linDirection: {},
      directionList: []
    }
  },
  mounted () {
    this.getSizeDirection()
    this.getListDirection(1, this.pageSize)
  },
  methods: {
    getListDirection (page, pageSize) {
      var _this = this
      var commonQueryData = {
        page: page,
        pageSize: pageSize
      }
      this.$axios.post('/api/direction/getListDirection',
        this.$qs.stringify({
          commonQueryData: JSON.stringify(commonQueryData)
        }))
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.directionList = result.result
            var len = _this.directionList.length
            _this.showEdit.splice(0)
            for (let i = 0; i < len; i++) {
              _this.showEdit.push(false)
            }
          }
        })
    },
    getSizeDirection () {
      var _this = this
      this.$axios.post('/api/direction/getSizeDirection')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.total = parseInt(result.result)
          }
        })
    },
    // 下方分页点击
    handleCurrentChange (page) {
      this.page = page
      this.getListDirection(page, this.pageSize)
    },
    handleEdit (index, row) {
      this.$set(this.showEdit, index, true)
      this.linDirection = JSON.parse(JSON.stringify(row))
    },
    handleCancle (index, row) {
      this.directionList.splice(index, 1, this.linDirection)
      this.$set(this.showEdit, index, false)
    },
    handleSave (index, row) {
      var _this = this
      // eslint-disable-next-line eqeqeq
      if (row.name != this.linDirection.name) {
        this.$set(this.showEdit, index, false)
        this.$axios.post('/api/direction/update',
          this.$qs.stringify({
            directionData: JSON.stringify(row)
          })
        )
          .then(function (response) {
            var data = response.data
            if (data.isSuccess) {
              _this.$message({
                type: 'success',
                message: '更新成功！',
                offset: 50
              })
            } else {
              _this.$message({
                type: 'error',
                message: '更新失败，请重试！',
                offset: 50
              })
            }
          })
      }
    },
    addCancle () {
      this.dialogVisible = false
    },
    addSave () {
      var _this = this
      this.$axios.post('/api/direction/add',
        this.$qs.stringify({
          name: this.name
        })
      ).then(function (response) {
        var data = response.data
        if (data.isSuccess) {
          _this.$message({
            type: 'success',
            message: '添加成功',
            offset: 50
          })
          _this.name = ''
          _this.dialogVisible = false
          _this.getListDirection(_this.page, _this.pageSize)
        } else {
          _this.$message({
            type: 'error',
            message: '添加失败，请重试！',
            offset: 50
          })
        }
      }).catch(function (eachCallback) {
        _this.$message({
          type: 'error',
          message: '添加失败，请重试！',
          offset: 50
        })
      })
    },
    handleDelete (index, row) {
      var _this = this
      this.$axios.post('/api/direction/delete',
        this.$qs.stringify({
          directionData: JSON.stringify(row)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '删除成功',
              offset: 50
            })
            _this.getListDirection(_this.page, _this.pageSize)
          } else {
            // eslint-disable-next-line eqeqeq
            if (data.code == 501) {
              _this.$message({
                type: 'error',
                message: '删除失败，该学习方向下还有所属分类，请您检查',
                offset: 50
              })
              // eslint-disable-next-line eqeqeq
            } else if (data.code == 502) {
              _this.$message({
                type: 'error',
                message: '删除失败，该学习方向下还有所属课程，请您检查',
                offset: 50
              })
            }
          }
        })
    }
  }
}
</script>

<style>

</style>
